/**
	韩飞 设定审批关联
**/
var _currentOrgId = null;
var _currentDeptId = null;
var _currentPosId = null;
var _currentRoleId = null;
var _activeNode = null;
var _orgInfoCache = []; //记录读取过的ORG对象
var _noPos = false;
//树的左键  (获取各种可以的值)
function BDTree_clickNode($node, $nameDom, $fireEvent) {
    _noPos = false;
    $nameDom.style.color = "red";
    _activeNode = $node;
    _currentDeptId = null;
    _currentPosId = null;
    _currentRoleId = null;
    _currentOrgId = null;
    if ($node.url=="O") { //组织机构
      _currentOrgId = _activeNode.key;
    } else if ($node.url=="D") { //部门
      _currentDeptId = _activeNode.key;
      _currentOrgId = BDTree.getParent(_currentDeptId).key;
    } else if ($node.url=="P") { //岗位
      _currentPosId = _activeNode.key;
      _currentDeptId = BDTree.getParent(_currentPosId).key;
      _currentOrgId = BDTree.getParent(_currentDeptId).key
    } else if ($node.url=="L") { //无岗位员工
      _noPos = true;
    } else {
    }
    if($node.key!="ROOT"){
	    var spid = $node.key;
	    var depid = BDTree.getParent($node.key).key;
	    reloadLister(spid,depid);
    }else{
    	$("#jsonTbo").children().remove();
    	$("#jsonTbo").append("<tr align='center'><td colspan='3'>请选择对应的行政审批项目</td></tr>");
    }
}
function BDTree_clickNodeChange($node, $nameDom) {
   $nameDom.style.color = "black";
}
//外部接口open方法
function BDTree_nodeOpen($key) {
	BDTree.openNodeOne($key);
}

/** 树加载完毕 */
function BDTree_loaded() {
	if (_activeNode==null) {
		BDTree.clickByIndex(0); //这里有问题
		BDTree_nodeOpen("ROOT");//调用接口open方法实现只展开一个节点
	} else {
		var d = BDTree.getDom(_activeNode.key);
		if (d==null) { //节点被删除了
			BDTree.clickByIndex(1);
		} else {
			d = BDTree.getName(_activeNode.key);
			if (d.style.color!="red") { //被重载了
				BDTree.clickNode(_activeNode.key);
			}
		}
	}
}
var appid,depmentid;
//重新读取设定表信息
function reloadLister(spid,depid) {
	var model = BDTree.getNodeModel(spid);
    if(BDTree.getNodeModel(spid).url=="D"){
    	appid = spid;
    	depmentid = depid;
    	comdwr.getDataInfo(spid,function(list){
		  	$("#jsonTbo").children().remove();
		  	if(list.length){
				for(var i=0;i<list.length;i++){
					var html = "<tr align='center' style='";
					if(list[i].nameen!=""&&list[i].nameen!=null){
						if(list[i].appcode==""||list[i].appcode==null){
							html += "background-color: #E1F2FC;";
						}
					}
					html += "cursor: hand' onmouseover=\"this.style.backgroundColor='#EEF7FD';\" onmouseout=\"this.style.backgroundColor='';\">";
					html += "<td><input type='checkbox' value='"+list[i].tabid+"' name='relevancy' "
					if(list[i].appcode!=""&&list[i].appcode!=null){
						 html +="checked";
					}
					html += " /></td>";
					html += "<td>"+list[i].taben+"</td>";
					html += "<td>"+list[i].tabcn+"</td>";
					html += "</tr>";
					
					$("#jsonTbo").append(html);
				}
			}
		});
    	__get("domFilterDesc").innerHTML = BDTree.getNodeModel(spid).name+"：";
    }else{
    	__get("domFilterDesc").innerHTML = "";
    	$("#jsonTbo").children().remove();
    	$("#jsonTbo").append("<tr align='center'><td colspan='3'>请选择对应的行政审批项目</td></tr>");
    }
    
}

//保存设定表信息
function doSaveSet() {
	var tabids = "";
	var rel = document.all["relevancy"];
	if(rel && rel.length){
		for(var i=0;i<rel.length;i++){
			if(rel[i].checked){
				tabids += rel[i].value + ",";
			}
		}
	}else{
		if(rel && rel.checked){
			tabids += rel.value + ",";
		}
	}
	if(tabids == ""){
		_alert("请选择相关数据");
		return false;
	}
	if(confirm("确认保存信息？")){
		var setdata = {};
	    setdata["appid"] = appid; //审批id
	    setdata["depid"] = depmentid; //部门id
	    setdata["sort"] = "tab"; //类别标识
	    setdata["tabids"] = tabids; //表唯一标识
		comdwr.doSaveSet(setdata,function(back){
		  	if(back){
		  		_alert("保存成功")
		  	}else{
		  		_alert("保存失败")
		  	}
		});
	}
}